<!--月考勤列表，某月根据班组筛选的人的出勤情况-->
<template>
  <div class="list-container">
    <div class="table-panel">
      <el-row class="table-operate1">
        <el-button
          type="primary"
          :disabled="info.personIds.length==0"
          @click="confirm"
        >
          确认考勤
        </el-button>
      </el-row>
      <el-row>
        <el-col :span="5" style="border-right:1px solid #e8e8e8">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </el-col>
        <!-- :selectable='selectEnable'-->
        <el-col :span="19">
          <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="45" :selectable='selectEnable'>
            </el-table-column>
            <el-table-column align="center" type="index" width="55" label="序号">
            </el-table-column>
            <el-table-column align="center" prop="personName" label="姓名">
            </el-table-column>
            <el-table-column v-if="companyId" align="center" prop="companyConfirm" label="确认状态">
              <template slot-scope="scope">
                <div v-if="scope.row.companyConfirm==1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column v-else align="center" prop="projectConfirm" label="确认状态">
              <template slot-scope="scope">
                <div v-if="scope.row.projectConfirm==1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column v-if="companyId" align="center" prop="teamLeaderConfirm" label="班组长确认状态">
              <template slot-scope="scope">
                <div v-if="scope.row.teamLeaderConfirm==1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column v-else align="center" prop="companyConfirm" label="劳务公司确认状态" min-width="100">
              <template slot-scope="scope">
                <div v-if="scope.row.companyConfirm==1">已确认</div>
                <div v-else>未确认</div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="总出勤">
              <template slot-scope="scope">
                <div>{{ scope.row.count }}天</div>
              </template>
            </el-table-column>
            <el-table-column align="center" prop="operater" label="结算人">
            </el-table-column>
            <el-table-column fixed="right" label="操作" align="center" min-width="140">
              <template slot-scope="scope">
                <el-button
                  @click.native.prevent="checkRow(scope.row)"
                  type="text"
                  icon="el-icon-search"
                  >查看</el-button
                >
                <!-- <el-button
                  @click.native.prevent="deleteRow(scope.row)"
                  type="text"
                  icon="el-icon-delete"
                  >删除</el-button
                > -->
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row class="page">
        <div class="page-tag">
          共{{ Math.ceil(totalData / searchForm.pageSize) }}页/{{
            totalData
          }}条数据
        </div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="searchForm.currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="searchForm.pageSize"
          layout="sizes, prev, pager, next, jumper"
          :total="totalData"
        >
        </el-pagination>
      </el-row>
    </div>
  </div>
</template>

<script>
import {
  getTree,
  getPersonByTeamId,
  getPersonByserviceCorporationId,
} from "@/api/worker";
import {
  getMonthAttendanceList,
  deleteMonthAttendanceList,
  corporationConfirm,
  projectConfirm
} from "@/api/attendance.js";
import store from '../../../../store'
export default {
  data() {
    return {
      sercchData: {
        date: null,
        name: null,
        attendanceType: -1,
      },
      searchForm: {
        currentPage: 1,
        pageSize: 10,
        date: null,
        name: null,
        // status: null,
        teamId: null,
        companyId:null,//是劳务公司进来就有值
        serviceCorporationId: null,//tree选择的劳务公司
        projectId:localStorage.getItem('projectId')
      },
      confirmOptions:[
        { value: -1, label: "全部" },
        { value: 0, label: "未确认" },
        { value: 1, label: "已确认" },
      ],
      attendanceTypeOptions: [
        { value: -1, label: "全部" },
        { value: 0, label: "按天" },
        { value: 1, label: "按小时" },
      ],
      flag: 1, //1--一级劳务公司，0--二级班组
      // currentPage: 1,
      // pageSize: 10,
      totalData: 0,
      treeData: [
        {
          label: "劳务公司 1",
          children: [
            {
              label: "班组 1-1",
            },
          ],
        },
        {
          label: "劳务公司 2",
          children: [
            {
              label: "班组 2-1",
            },
            {
              label: "二级 2-2",
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
            },
            {
              label: "二级 3-2",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "name",
      },
      tableData: [],
      info:{
        date:null,
        personIds:[]
      },
      companyId:null
    };
  },
  created() {
    // console.log(this.$route)
    console.log(this.info.personIds);
    this.companyId = localStorage.getItem('companyId')
    this.searchForm.date = this.$route.query.date;
    this.info.date = this.$route.query.date;
    this.getTreeData();
    this.getTableData();
  },
  methods: {
    selectEnable(row, rowIndex) {
      if(this.companyId){//劳务公司进来
        if(row.teamLeaderConfirm){
          return true
        }else return false
      }else{//项目方进来
        if(row.companyConfirm){
          return true
        }else return false
      }
    },
    confirm(){
      this.$confirm(
        "是否确认选中人员的月考勤?",
        "月考勤确认",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(()=>{
        if(this.companyId){
        corporationConfirm(this.info).then(res=>{
          console.log(res);
            this.$message({
            type: "success",
            message: "确认成功！",
          });
          this.getTableData()
        }).catch(res=>{
          console.log(res);
            this.$message({
            type: "error",
            message: res.message,
          });
        })
      }else{
        projectConfirm(this.info).then(res=>{
          this.$message({
            type: "success",
            message: "确认成功！",
          });
          this.getTableData()
        }).catch(err=>{
          console.log(err.message);
            this.$message({
              type: "error",
              message: err.message,
            });
          })
        }
      }).catch(()=>{
          this.$message({
            type: "info",
            message: "已取消确认",
          });
        })
    },
    handleSelectionChange(res){
      var personIds = []
      res.forEach((item)=>{
        personIds.push(item.personId)
      })
      this.info.personIds=personIds
      // console.log(personIds);
      console.log(this.info.personIds);
    },
    // 获取劳务公司、班组树形数据
    getTreeData() {
      getTree().then((res) => {
        // console.log(res)
        this.treeData = res.data.data;
      });
    },
    onSubmit() {
      this.getTableData();
    },
    //发送请求获取表格数据
    getTableData() {
      if (this.flag === 0) {
        // 根据班组id查询班组人员————选择班组
        getMonthAttendanceList(this.searchForm).then((res) => {
          console.log(res.data.data.records);
          var records=res.data.data.records
          var tableData = []
          if(this.companyId){//劳务公司进来
            records.forEach(item=>{
              if(!item.companyConfirm){//劳务公司未确认
                  tableData.push(item)
              }
            })
          }else{//项目方进来
            records.forEach(item=>{
              if(!item.projectConfirm){//项目方未确认
                  tableData.push(item)
              }
            })
          }
          this.tableData = tableData
          this.totalData = res.data.data.total;
          // this.tableData = res.data.data.records;
          // this.processTableData();
        });
      } else {
        // 根据劳务公司id查询劳务公司人员————选择劳务公司
        getMonthAttendanceList(this.searchForm).then((res) => {
          console.log(res.data.data.records);
          var records=res.data.data.records
          var tableData = []
          if(this.companyId){//劳务公司进来
            records.forEach(item=>{
              if(!item.companyConfirm){//劳务公司未确认
                  tableData.push(item)
              }
            })
          }else{//项目方进来
            records.forEach(item=>{
              if(!item.projectConfirm){//项目方未确认
                  tableData.push(item)
              }
            })
          }
          this.tableData = tableData
          this.totalData = res.data.data.total;
          // this.tableData = res.data.data.records;
          // this.processTableData();
        });
      }
    },
    // 树状图
    handleNodeClick(res) {
      // console.log(res)
      if (res.children == null) {
        // console.log("班组")
        this.flag = 0;
        this.searchForm.teamId = res.id;
        this.searchForm.serviceCorporationId = null;
        this.getTableData();
      } else {
        // console.log("劳务公司")
        this.flag = 1;
        this.searchForm.serviceCorporationId = res.id;
        this.searchForm.teamId = null;
        this.getTableData();
      }
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val;
      this.getTableData();
    },
    checkRow(res) {
      this.$router.push({
        path: "/attendance/month/detail",
        query: {
          personInfo: {
            personId: res.personId,
            personName: res.personName,
            serviceCorporationName: res.serviceCorporationName,
            teamName: res.teamName,
          },
          date: this.searchForm.date,
        },
      });
    },
    deleteRow(res) {
      deleteMonthAttendanceList({
        date: res.date,
        personId: res.personId,
      }).then((e) => {
        this.getTableData();
        this.$message({
          type: "success",
          message: "删除成功！",
        });
      });
    },
  },
};
</script>
<style scoped>
.table-operate1{
  margin-bottom: 10px;
  text-align: right;
}
</style>